/**
 * Created by zhanglihe on 15/11/19.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'dialog',
    'model'
], function ($, common, ready, Collection, template, dialog, Model) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'send_bonus',
                condition: {limit: 10, offset: 0},
                model: {
                    event: {
                        //'.add-bonus:click': 'add-bonus',
                    },
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '发放奖励',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function () {
                        //todo   js验证

                        _this.data.phone = $("input[name='phone']").val();
                        _this.data.amount = $("input[name='amount']").val();
                        _this.data.bonus_type = $("#bonus_type").val();
                        _this.data.is_withdraw = _this.data.bonus_type == 4 ? 2 : $("input[name='is_withdraw']:checked").val();
                        _this.data.remarks = $("input[name='remarks']").val();
                        if (new Model({
                                url: _global.url.api + 'send_bonus',
                                data: _this.data,
                                valid_config: {
                                    phone: 'required',
                                    amount: 'required',
                                    bonus_type: 'required',
                                    is_withdraw: 'required',
                                    remarks: 'required',
                                },
                            }).post()) {
                            _this.collection.reload();
                        }


                    }, cancel: function () {
                    },

                });
                $('select[name=bonus_type]').on('change', function (e) {
                    var bonus_type = $(this).val();
                    if (bonus_type == 4) {
                        $('#canWithdraw').hide();
                    } else {
                        $('#canWithdraw').show();
                    }
                });
                _this.dialog.show();

                //$('input,select,textarea').on('change', function(e) {
                //    _this.change(e);
                //});

            });
        }
    };

    new index();

});